package com.itany.cms.controller;

import ch.qos.logback.core.model.Model;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.itany.cms.constant.ResponseCodeConstant;
import com.itany.cms.entity.CourseType;
import com.itany.cms.entity.Resource;
import com.itany.cms.entity.User;
import com.itany.cms.exception.RequestParameterErrorException;
import com.itany.cms.service.CourseTypeService;
import com.itany.cms.service.ResourceService;
import com.itany.cms.service.UserService;
import com.itany.cms.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/resource")
public class ResourceController {
    @Autowired
    private ResourceService resourceService;
    @Autowired
    private UserService userService;

    @RequestMapping("/findResource")
    public ModelAndView findResource(Resource resourceParam, String pageNo, String pageSize) {
        ModelAndView mav = new ModelAndView();
        List<User> users = userService.findAll();
        mav.addObject("users",users);
        PageInfo<Resource> resourcePage = resourceService.findByParams(resourceParam,pageNo, pageSize);
        mav.addObject("resourcePage", resourcePage);
        mav.setViewName("backend/back_resourceSet");
        return mav;
    }
    @ResponseBody
    @RequestMapping("/modifyStatus")
    public ResponseResult modifyStatus(String id, String status) {
        ResponseResult result = new ResponseResult();
        try {
            resourceService.modifyStatus(id,status);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        }catch (Exception e) {
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }
    @RequestMapping ("/detail")
    public ModelAndView resourceDetail(@RequestParam("id") String  id, @RequestParam("path") String  path, String pageNo, String pageSize) {
        Resource resourceParam = new Resource();
        resourceParam.setId(Integer.parseInt(id));
        resourceParam.setPath(path);
        ModelAndView mav = new ModelAndView();
        PageInfo<Resource> resourcePage = resourceService.findDetail(resourceParam,pageNo, pageSize);
        mav.addObject("resourcePage", resourcePage);
        mav.setViewName("backend/back_resourceDetailSet");
        return mav;
    }
}
